package Lab10.Q3;

import java.io.*;

public class RMCMTS {
    public static void main(String[] args) throws IOException {
        String input = "./input.txt";
        String output = "./output.txt";
        removeComments(input, output);
    }

    public static void removeComments(String inputPath, String outPath) throws IOException {
        File inputFile = new File(inputPath);
        File outputFile = new File(outPath);
        BufferedReader reader = null;
        FileWriter writer = null;
        StringBuilder output = new StringBuilder();
        try {
            reader = new BufferedReader(new FileReader(inputFile));
            String line, tmp;
            boolean lineComment = false;
            while ((line = reader.readLine()) != null) {
                tmp = line;
                if (tmp.contains("/*")) {// //情况
                    lineComment = true;
                    tmp = tmp.substring(0, tmp.indexOf("/*"));
                    if (!tmp.trim().isEmpty()) //如果去掉注释只剩空格
                        output.append(tmp).append('\n');
                } else if (tmp.contains("*/")) {// //情况
                    lineComment = false;
                    tmp = tmp.substring(tmp.indexOf("*/") + 1, tmp.length() - 1);
                    if (!tmp.trim().isEmpty()) //如果去掉注释只剩空格
                        output.append(tmp).append('\n');
                } else if (lineComment) {
                } else if (tmp.contains("//")) {// //情况
                    tmp = tmp.substring(0, tmp.indexOf("//"));
                    if (!tmp.trim().isEmpty())//如果去掉注释只剩空格
                        output.append(tmp).append('\n');
                } else
                    output.append(tmp).append('\n');
            }

            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            writer = new FileWriter(outputFile, false);//覆盖
            writer.append(output);
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}